package cn.tedu.store.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.tedu.store.entity.Address;
import cn.tedu.store.entity.Order;
import cn.tedu.store.entity.OrderItem;
import cn.tedu.store.mapper.OrderMapper;
import cn.tedu.store.service.IAddressService;
import cn.tedu.store.service.ICartService;
import cn.tedu.store.service.IOrderService;
import cn.tedu.store.service.ex.AccessDeniedException;
import cn.tedu.store.service.ex.InsertException;
import cn.tedu.store.vo.CartVo;

@Service
public class OrderServiceImpl implements IOrderService {
	@Autowired
	private OrderMapper orderMap;
	@Autowired
	private IAddressService addService;
	@Autowired
	private ICartService cartService;
	
	@Override
	@Transactional
	public Order createOrder(Integer aid, Integer[] cids, Integer uid, String username) {
		// 通过参数aid查询收货地址数据
			Address result = addService.getByAid(aid);
		// 检查数据归属是否正确
			if(!result.getUid().equals(uid)) {
				throw new AccessDeniedException("数据归属异常!");
			}
	    // 通过参数cids查询购物车数据，得到List<CartVO>
			List<CartVo> list = cartService.getByCarts(cids, uid);
	    // 遍历List<CartVO>，计算商品总价
			Long totalPrice = 0L;
			for (CartVo cartVo : list) {
				//总价
				 totalPrice += cartVo.getNum() * cartVo.getRealPrice();
			}

	    // 创建当前时间对象now
			Date date = new Date();
	    // 创建Order对象
			Order order = new Order();
	    // 封装Order属性：uid
			order.setUid(uid);
	    // 封装Order属性：recv_???
			order.setRecvName(result.getName());
			order.setRecvAddress(result.getAddress());
			order.setRecvProvince(result.getProvinceName());
			order.setRecvCity(result.getCityName());
			order.setRecvArea(result.getAreaName());
			order.setRecvPhone(result.getPhone());
	    // 封装Order属性：total_price(以上计算的商品总价)
			order.setTotalPrice(totalPrice);
	    // 封装Order属性：status(0)
			order.setStatus(0);
	    // 封装Order属性：order_time(now)
			order.setOrderTime(date);
	    // 封装Order属性：pay_type(null), pay_time(null)
			order.setPayType(null);
			order.setPayTime(null);
	    // 封装Order属性：4项日志
			order.setCreatedUser(username);
			order.setCreatedTime(date);
			order.setModifiedUser(username);
			order.setModifiedTime(date);
	    // 向t_order表中插入数据：saveOrder(Order order)
			InsertOrder(order);
	    // 遍历List<CartVO>
			for (CartVo cartVo : list) {
				// -- 创建OrderItem对象
				OrderItem orderitem = new OrderItem();
		    // -- 封装OrderItem属性：oid(order.getOid())
				orderitem.setOid(order.getOid());
		    // -- 封装OrderItem属性：pid,title,image,price,num
				orderitem.setPid(cartVo.getPid());
				orderitem.setTitle(cartVo.getTitle());
				orderitem.setImage(cartVo.getImage());
				orderitem.setPrice(cartVo.getPrice());
				orderitem.setNum(cartVo.getNum());
		    // -- 封装OrderItem属性：4项日志
				orderitem.setCreatedUser(username);
				orderitem.setCreatedTime(date);
				orderitem.setModifiedUser(username);
				orderitem.setModifiedTime(date);
		    // -- 向t_order_item表中插入若干条数据：saveOrderItem(OrderItem orderItem)
				InsertOrderItem(orderitem);
			}
	    
	    // 返回order对象，返回之前可以将部分数据设置为null
			order.setCreatedUser(null);
			order.setCreatedTime(null);
			order.setModifiedUser(null);
			order.setModifiedTime(null);
		return order;
	}
	
	/**
	 * 插入信息到 订单表
	 * @param order
	 * @return
	 */
	private Integer InsertOrder(Order order) {
		Integer row = orderMap.InsertOrder(order);
		System.err.println("++++++++++++++row:  "+row);
		if(row !=1) {
			throw new InsertException("插入数据失败！");
		}
		return row;
	};
	
	/**
	 * 插入信息到订单商品表
	 * @param orderitem
	 * @return
	 */
	private Integer InsertOrderItem(OrderItem orderitem) {
		Integer row = orderMap.InsertOrderItem(orderitem);
		if(row !=1) {
			throw new InsertException("插入数据失败！！！");
		}
		return row;
	};

}
